package cn.josh.action;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@WebServlet("/CheckCoedServlet")
public class CheckCoedServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	{

		//取得前端数据     request域得到
		String inputCode = request.getParameter("checkCode");
		System.out.println("验证码：" +  inputCode);
		
		//session域：当次会话（只要浏览器没有关闭都能访问到）
		//从session域取得验证码
		HttpSession session = request.getSession();
		String seeCode = (String)session.getAttribute("sesscode");
		System.out.println("sestion中的验证码:" + seeCode);
		
		//检验两个验证码（不区分大小写）
		if(inputCode.equalsIgnoreCase(seeCode))
		{
			//跳出对话框
			response.getWriter().append("<script>alert('注册成功！');</script>");
			request.getRequestDispatcher("/RegisterServlet").forward(request, response);;
			//重定向到首页
			//response.sendRedirect("/ShoppingWebsite/jsp/index.jsp");
			
		}
		else
		{
			response.getWriter().append("<script>alert('验证码错误！');window.location.href='/ShoppingWebsite/jsp/register.jsp'</script>");
			//重定向到首页
			//response.sendRedirect("/ShoppingWebsite/jsp/register.jsp");
		}
	}
 
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	{
		doGet(request, response);
	}

}
